import {createRouter, createWebHistory} from 'vue-router'
import {routes} from 'vue-router/auto-routes'
//自动化路由配置
import page from "./page.js"

//手动添加特殊的路由
//删除掉没有的路由
//也可以不删
let route = routes.filter((item) => {
    return item.path !== "/error"
});

//添加路由
route = [
    ...route,
    //有些特殊的地址可以在这里添加
    {
        path: '/:catchAll(.*)',
        name: '404',
        component: () => import("../pages/error/404.vue")
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes: route
})
console.log({"路由":route})
//自定义路由
page.forEach(e => {
    let {
        routerName,
        redirect,
        title
    } = e
    let {matched} = router.resolve(routerName);
    let data = matched[matched.length - 1];
    data.redirect = redirect
    data.meta.title = title
})

//把网页标题重命名
router.beforeEach((to, from, next) => {
    // console.log(to.name);
    document.title = to.meta.title;
    next()
})

export default router;